package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.ApartmentInfoService;
import com.atguigu.lease.web.admin.service.RoomAttrValueService;
import com.atguigu.lease.web.admin.service.RoomInfoService;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    //private ApartmentInfoService apartmentInfoService ;
    private ApartmentInfoMapper apartmentInfoMapper ;

    @Autowired
    private RoomAttrValueMapper roomAttrValueMapper ;

    @Autowired
    private RoomFacilityMapper roomFacilityMapper ;

    @Autowired
    private RoomLabelMapper roomLabelMapper ;

    @Autowired
    private RoomPaymentTypeMapper roomPaymentTypeMapper;

    @Autowired
    private RoomLeaseTermMapper roomLeaseTermMapper ;

    @Autowired
    private GraphInfoMapper graphInfoMapper ;

    @Override
    public IPage<RoomItemVo> pageItem(Page page, RoomQueryVo queryVo) {
        return baseMapper.getRoomItemVoList2(page,queryVo);
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {

        RoomDetailVo roomDetailVo = new RoomDetailVo();
        //1.查询房间的基本信息
        RoomInfo roomInfo = getById(id);
        BeanUtils.copyProperties(roomInfo,roomDetailVo);

        //2.查询房间所属的公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
        roomDetailVo.setApartmentInfo(apartmentInfo);

        //3.查询房间的属性信息(属性信息包含两部分：属性名和属性值)
        List<AttrValueVo> attrValueVoList =  roomAttrValueMapper.selectRoomAttrValueListByRoomId(id);
        roomDetailVo.setAttrValueVoList(attrValueVoList);

        //4.查询房间的配套信息
        List<FacilityInfo> facilityInfoList = roomFacilityMapper.selectFacilityListByRoomId(id);
        roomDetailVo.setFacilityInfoList(facilityInfoList);

        //5.查询房间的标签信息
        List<LabelInfo> labelInfoList = roomLabelMapper.selectLabelInfoListByRoomId(id);
        roomDetailVo.setLabelInfoList(labelInfoList);

        //6.查询房间的支付方式信息
        List<PaymentType> paymentTypeList = roomPaymentTypeMapper.selectPaymentTypeListByRoomId(id);
        roomDetailVo.setPaymentTypeList(paymentTypeList);

        //7.查询房间的可选租期信息
        List<LeaseTerm> leaseTermList = roomLeaseTermMapper.selectLeaseTermListByRoomId(id);
        roomDetailVo.setLeaseTermList(leaseTermList);

        //8.查询房间的图片列表
        List<GraphInfo> graphInfoList = graphInfoMapper.selectList(new LambdaQueryWrapper<GraphInfo>()
                .eq(GraphInfo::getItemType, ItemType.ROOM)
                .eq(GraphInfo::getItemId,id)
        );
        roomDetailVo.setGraphVoList(graphInfoList.stream()
                .map(graphInfo -> GraphVo.builder()
                                            .name(graphInfo.getName())
                                            .url(graphInfo.getUrl()).build()).collect(Collectors.toList()));

        return roomDetailVo;
    }

    @Override
    public RoomDetailVo getDetailById2(Long id) {
        return baseMapper.getDetailById(id);
    }
}




